Repository navigation
Fix the leading_zeros constant for 8, 16 and 64-bit integers - #702
Merged
Merged
Conversation
greenhat
requested review from
Firestar99,
LegNeato and
eddyb
as code owners
November 15, 2026 12:50
Firestar99
force-pushed
the
fix-leading-zeros
branch
2 times, most recently
from
November 16, 2026 13:08
0071a3d to
7df8d33
Compare
Firestar99
approved these changes
Nov 16, 2026
Firestar99
left a comment
Member
There was a problem hiding this comment.
- cleaned up AI-isms
- merged your new tests with the existing
trailing_zerostest intobitops, since it was an almost 1:1 copy - in a second commit, cleaned up the leading/trailing zeros impl to make it simpler to read
As the difftests pass, I'll just self-approve this one
Firestar99
force-pushed
the
fix-leading-zeros
branch
from
November 16, 2026 13:16
7df8d33 to
720cad3
Compare
`find_xsb` subtracted `offset` from the leading zeros of the 32-bit word instead of adding it. `leading_zeros` returned 48 too many for a non-zero `u8`, 32 too many for a non-zero `u16` (both since 05042d1) and 32 too few for a non-zero `u64`. Add `offset`, and pass 32 for the lower half of a 64-bit value and 0 for the higher half. Add the difftest `lang/core/intrinsics/leading_zeros` that runs `leading_zeros`, `leading_ones`, `checked_ilog2` and `checked_next_power_of_two` on 8, 16, 32 and 64-bit values in a shader and compares the results with the CPU.
Firestar99
force-pushed
the
fix-leading-zeros
branch
from
November 16, 2026 13:55
720cad3 to
f90433d
Compare
Firestar99
enabled auto-merge
November 16, 2026 15:13
Firestar99
disabled auto-merge
November 16, 2026 16:05
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Close #701
leading_zerosreturns a wrong value in a shader for every non-zerou8,u16andu64:leading_ones,checked_ilog2andchecked_next_power_of_twocall it, so they are wrong as well.u32is right.This fixes the constant that the
FindUMsbresult is subtracted from (find_xsbinbuilder/intrinsics.rs). The code emitted foru32does not change.The new difftest
lang/core/intrinsics/leading_zerosrunsleading_zeros,leading_ones,checked_ilog2andchecked_next_power_of_twoon 8, 16, 32 and 64-bit values in a shader and compares the results with the CPU. It fails without the fix.The fix and the test were written with an AI coding agent. I reviewed and checked it.
Details
The code emitted before the fix for a
u8and for au64(lower half, then higher half):The constants are now 7 for
u8, 15 foru16, and 63 and 31 for the two halves of au64.For
u8andu16the constant became31 - offsetin 05042d1. Foru64the 32 was on the higher half since 95d2902. 7ad4c4d fixed the same offsets fortrailing_zeros.The compiletest
ui/lang/core/intrinsics/leading_zeros.rshas the right asserts for all four widths, but it isbuild-passonly, so they never run.The test reads the values from a buffer and narrows them in the shader. It follows
trailing_zeros_64: Ash backend (it needsInt8,Int16andInt64), skipped on macOS. I ran it on lavapipe only.